package com.yanqu.road.server.cmd;

import com.yanqu.road.connect.AgentServerLinkControl;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TonBingProto;
import com.yanqu.road.player.Player;
import com.yanqu.road.player.PlayerControl;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.netty.channel.ChannelHandlerContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.A_TON_BING_TRANSFER, desc = "巅峰统兵演武消息转发")
public class TonBingTransferCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {

        TonBingProto.TonBingAgentTransferMsg agentTransferMsg = TonBingProto.TonBingAgentTransferMsg.parseFrom(packet.getBytes());
        int code = agentTransferMsg.getCode();
        List<Long> userIdList = agentTransferMsg.getUserIdList();
        byte[] msgBytes = agentTransferMsg.getMsg().toByteArray();

        Map<Long, Player> allPlayer = new HashMap<>(PlayerControl.getOnlinePlayerMap());

        for (long userId : userIdList) {
            if (allPlayer.containsKey(userId)) {
                YanQuMessage message = YanQuMessageUtils.buildMessageByte(code, msgBytes);
                message.setPlayerId(userId);
                message.setLen(packet.getLen());
                AgentServerLinkControl.routeClient(message);
            }
        }

    }
}
